var t = getApp(),
    e = t.requirejs('core');
Page({
    data: {
        date: "",
        time: "",
        timeList: [],
        num: 1,
        reseult: "",
        id: '',
        shopid: "",
        merch: {},
        xiang: {},
        memberCode: '',
        is_hui: 0
    },
    bindMemberCode(s) {
        this.setData({
            memberCode: s.detail.value
        })
    },
    onLoad(s) {
        console.clear();
        this.setData({id: s.id, shopid: s.shopid});
        e.get("syt.merch.credit_detail1", s, res => {
            this.setData({merch: res.merch, xiang: res.xiang, reseult: res.xiang.xjiage, is_hui: res.is_hui})
        })
        
    },
    // 修改日期
    changDate(s) {
        let date = new Date();
        let now = new Date(date.getFullYear(), parseInt(date.getMonth()), date.getDate()).getTime();
        let select = new Date(s.detail.value.split('-')).getTime();
        if (select < now) {
            wx.showToast({
                title: '不能选择今天之前的日期',
                icon: 'none'
            })
            this.setData({
                date: `${date.getFullYear()}-${String(parseInt(date.getMonth()) + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
            })
            this.getTime();
            return
        }
        this.setData({
            date: s.detail.value
        })
        this.getTime();
    },
    // 获取时间
    getTime() {
        e.get("syt.merch.get_yue_time", {date: this.data.date}, res => {
            let list = res.yu.filter(item => {
                return item.can_yue == 1
            });
            this.setData({timeList: list})
            if(list.length == 0) {
                wx.showToast({title: '预约已满，请更换日期', icon: 'none'})
            }
        })
    },
    // 修改时间
    changeTime(s) {
        console.log(s)
        this.setData({
            time: s.detail.value
        })
    },
    add() {
        this.setData({
            num: this.data.num + 1
        })
        this.calculate();
    },
    sub() {
        this.setData({
            num: this.data.num - 1
        })
        this.calculate();
    },
    calculate() {
        this.setData({
            reseult: parseFloat(this.data.xiang.xjiage) * this.data.num
        })
    },
    submit(s) {
        let data = s.detail.value;
        data.nowTime = data.time && this.data.timeList[data.time].title || '';
        data.total = this.data.num;
        data.id = this.data.id;
        data.mechid = this.data.shopid;
        data.price = this.data.reseult;
        data.memberCode = this.data.memberCode;
        if(!data.date) {
            wx.showToast({title: '请选择日期', icon: 'none'})
            return
        }
        if(!data.time) {
            wx.showToast({title: '请选择时间', icon: 'none'})
            return
        }
        e.get("syt.merch.yue_submit", data, res => {
            console.log("提交", res)
            if(res.error != 0) {
                wx.showToast({title: res.message, icon: 'none'});
                return
            } else {
                wx.navigateTo({
                  url: '/pages/order/pay/index1?id=' + res.id
                })
            }
        })
    }
})